#include "base/log.h"

#include "base/json.h"
namespace net_tools::base{

Json::Json()
  :write_(nullptr),
   read_(nullptr)
{
  ::Json::StreamWriterBuilder writerbuilder;
  write_ = writerbuilder.newStreamWriter();
  ::Json::CharReaderBuilder readerbuilder;
  read_ = readerbuilder.newCharReader();
}
Json::~Json(){
  if(ifs_.is_open()){
    ifs_.close();
  }
  if(ofs_.is_open()){
    ifs_.close();
  }
  delete write_;
  delete read_;
}
bool Json::Open_read(std::string address){
  ifs_ = std::ifstream(address.c_str());
  return ifs_.is_open();
};
bool Json::Open_write(std::string address){
  ofs_ = std::ofstream(address.c_str());
  return ofs_.is_open();
};
void Json::Json_write(::Json::Value* root){
  write_->write(*root,&ofs_);
};
void Json::Read_from_ifs(::Json::Value* root){
  std::stringstream ss;
  ss << ifs_.rdbuf();
  std::string json = ss.str();
  read_->parse(json.c_str(),json.c_str() + json.length(),root,nullptr);
};

} // namespace net_tools::base